<?php
/**
 *栏目管理
 *@author huuwhois
 *@time 2017/11/17
*/

namespace app\admin\controller;

use app\admin\model\Article as article_model;
use think\Request;
use think\Db;

class Article extends Base
{
    protected $article_model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $article_model = new article_model();
        $this->article_model = $article_model;
    }


    public function index($cate_id = "")
    {
        return $this->listing($cate_id);
    }

    public function mylist($cate_id = "")
    {
        return $this->listing($cate_id, $this->user_id);
    }

    public function searcher($cate_id = '', $uid = '')
    {
        $map=array();
        // var_dump(input('get.'));exit;
        $searcher = input('get.');
        $logmin = $searcher['logmin'].'0:0:0';
        $logmax = $searcher['logmax'].'23:59:59';
        $title =  $searcher['title'];
        if ($logmin != '' || $logmax != '') {
            $logmin = $logmin != '' ? strtotime($logmin) :0;
            $logmax = $logmax != '' ? strtotime($logmax) : time();
            $map['pubtime'] = ['between',$logmin.','.$logmax];
        }
        if ($title) {
            $map['title'] = ['like','%'.$title.'%'];
        }

        return $this->listing($cate_id = '', $uid = '', $map, $searcher);
    }
//文章列表
    public function listing($cate_id = "", $uid = "", $map = array(), $searcher = '')
    {
        
        $page_counts = 20;
        if ($uid) {
            $map['uid'] = $uid;
        }
        if ($cate_id) {
            $map['cate_id'] = $cate_id;
        }

        if (empty($map)) {
            $counts = $this->article_model->count('id');
            $data = $this->article_model->field('id,uid,cate_id,thumb,title,source,writer,pubtime,sort,ispost,dutytime,clicks')->order('id', 'desc')->paginate($page_counts, $counts);
        } else {
            $counts = $this->article_model->where($map)->count('id');
            $data = $this->article_model->where($map)->field('id,uid,cate_id,thumb,title,source,writer,pubtime,sort,ispost,dutytime,clicks')->order('id', 'desc')->paginate($page_counts, $counts);
        }

        $pages = intval(ceil($counts/$page_counts));
        $category_name_tree = category_name_tree();

        $this->assign('searcher', $searcher);
        $this->assign('searcher', $searcher);
        $this->assign('cate_id', $cate_id);
        $this->assign('category_name_tree', $category_name_tree);
        $this->assign("pages", $pages);
        $this->assign('counts', $counts);
        $this->assign('data', $data);
        return $this->fetch('listing');
    }

//添加文章
    public function add($cate_id = '')
    {
        if (Request::instance()->isPOST()) {
            $uid=$this->user_id;
            // var_dump(input("post."));
            // exit;

        //文章信息表
            $this->article_model->uid=$uid;
            $this->article_model->cateid = intval(input('post.cid'));
            $this->article_model->tplid = intval(input('post.template'));
            $this->article_model->title = trim(input('post.title'));
            $this->article_model->writer = ( null!==input('post.writer') ) ? input('post.writer'):$this->user_name;
            $this->article_model->source = ( null!==input('post.source') ) ? input('post.source'):"原创";
            $this->article_model->islev_msg = intval(input('post.islev_msg'));
            $this->article_model->sort = intval(input('post.sort'));
            $this->article_model->thumb = input('post.thumb');
            $this->article_model->pubtime = time();
        //文章内容表
            $keywords = input('post.keywords');
            $description = input('post.description');
            $body = input('post.editorValue');
            Db::startTrans();
            try {
                $this->article_model->save();
                $aid = $this->article_model->id;
                $data = ['aid'=>$aid,'keywords'=>$keywords,'description'=>$description,'body'=>$body];
                $content = new \app\admin\model\ArticleContent($data);
                $content->save();
                Db::commit();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);
                // echo $msg;exit;
                Db::rollback();
                $this->error("错误代码:".$msg);
            }
                $this->success('添加成功', 'index');
        } else {
            $top = model('category')->categoryChildren();
            $this->assign('data', $top);

            $this->assign('cate_id', intval($cate_id));
            return $this->fetch();
        }
    }

//文章修改
    public function edit($id = "")
    {
        $data = $this->article_model->get($id);
        $artcile_content = model('ArticleContent')->get($data->id);
        if (Request::instance()->isPOST()) {
            // var_dump(input('post.'));
            // var_dump($id);exit;
            $data->cate_id = intval(input('post.cid'));
            $data->tpl_id = intval(input('post.template'));
            $data->title = trim(input('post.title'));
            $data->writer = input('post.writer');
            $data->source = input('post.source');
            $data->islev_msg = intval(input('post.islev_msg'));
            $data->sort = intval(input('post.sort'));
            $data->thumb = input('post.thumb');
            $data->pubtime = time();
        //文章内容表
            $artcile_content->keywords = input('post.keywords');
            $artcile_content->description = input('post.description');
            $artcile_content->body = input('post.editorValue');
            Db::startTrans();
            try {
                $this->article_model->save();
                $artcile_content->save();
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);
                // echo $msg;exit;
                $this->error("错误代码:".$msg);
            }
                $this->success('编辑成功', 'index');
        } else {
            $category_data = model('category')->categoryChildren();
            $artcile_content = model('ArticleContent')->find($data->id);
            // var_dump($data);exit;
            $this->assign('category_data', $category_data);
            $this->assign('artcile_content', $artcile_content);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    //文章审核
    public function duty($id = '', $ispost = 0)
    {
        if (Request::instance()->isAjax()) {
            $time = time();
            $article = $this->article_model->get($id);
            $article->ispost = $ispost;
            $article->dutyadmin = $this->user_id;
            $article->dutytime = $time;
            $date = date('Y-m-d H:i:s', $time);
            if ($article->save()) {
                return ['status'=>1,'msg'=>'审核成功','time'=>$date];
            } else {
                return ['status'=>0,'msg'=>'审核失败'];
            }
        }
    }

    //查看文章
    public function preview($id = '')
    {
        $infodata = $this->article_model->field('id,cate_id,thumb,title,source,writer,pubtime,sort,clicks')->find($id);
        $bodydata = Db::table("dean_article_content")->where('aid', $id)->field('body')->find();

        $this->assign('infodata', $infodata);
        $this->assign('bodydata', $bodydata);
        return $this->fetch();
    }

//(批量)删除文章
    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            if ($this->article_model->destroy($id)) {
                return ['status'=>1];
            } else {
                return ['status'=>0];
            }
        }
    }
}
